package 动态规划;

public class 两数相加 {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        ListNode dummyHead = new ListNode(-1);
        ListNode root = dummyHead;
        int go = 0;
        while (l1 != null || l2 != null || go != 0){
            int l1Val = l1 == null ? 0 : l1.val;
            int l2VAl = l2 == null ? 0 : l2.val;

            int sum = (l1Val + l2VAl + go) % 10;
            go = (l1Val + l2VAl + go) / 10;

            l1 = l1 == null ? l1 : l1.next;
            l2 = l2 == null ? l2 : l2.next;

            root.next = new ListNode(sum);
            root = root.next;
        }
        return dummyHead.next;

    }

    public static  class ListNode {
      int val;
      ListNode next;
      ListNode() {}
      ListNode(int val) { this.val = val; }
      ListNode(int val, ListNode next) { this.val = val; this.next = next; }
  }
}
